/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2.util;


import java.io.Serializable;
import java.util.Vector;

/**
 * A blocking queue based on java.util.Vector
 * @author Diego Bonura
 */
public class VectorQueue<T> implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = -3830697404185323064L;
	protected Vector<T> vector;

    /**
     * Constructs a vectorQueue from a normal vector.
     * 
     * @param vector
     *            The vector
     */
    public VectorQueue(Vector<T> vector) {
        this.vector = vector;
    }

    /**
     * Constructs an empty vectorQueue.
     */
    public VectorQueue() {
        this(new Vector<T>());
    }

    /**
     * Appends a new message to the queue and notify all the threads waiting
     * 
     * @param o
     *            The new message
     * @return Always true
     */
    public boolean append(T o) {

        synchronized (vector) {
            vector.addElement(o);
            vector.notifyAll();
        }

        return true;
    }

    /**
     * Returns if the vector queue is empty.
     * 
     * @return true if it's empty false otherwise.
     */
    public boolean isEmpty() {
        boolean result;
        synchronized (vector) {
            result = vector.isEmpty();
        }

        return result;

    }

    /**
     * Gets the message into the queue, if the queue is empty return null
     * 
     * @return The message or null
     */
    public T poll() {
        synchronized (vector) {
            if (vector.size() > 0) {
                T o = vector.elementAt(0);

                vector.removeElementAt(0);

                return o;
            }
        }

        return null;
    }

   

    /**
     * Returns the first message stored into the queue, if the queue is empty
     * waits the threads until one notify
     * 
     * @return The first message
     * @throws InterruptedException
     */
    public T pull() throws InterruptedException {
        return pull(0);
    }

    /**
     * Returns the first message stored into the queue, if the queue is empty
     * waits the threads until one notify or timeout expired
     * 
     * @param timeout
     *            The timeout
     * @return The first message or null
     * @throws InterruptedException
     */
    public T pull(long timeout) throws InterruptedException {
        if (timeout < 0) {
            throw new IllegalArgumentException("timeout < 0");
        }

        long stopTime = System.currentTimeMillis() + timeout;

        T o = null;

        while ((o = poll()) == null) {
            if (timeout == 0) {
                synchronized (vector) {
                    vector.wait();
                }
            } else {
                long now = System.currentTimeMillis();

                if (stopTime > now) {
                    synchronized (vector) {
                        vector.wait(stopTime - now);
                    }
                } else {
                    break;
                }
            }
        }

        return o;
    }

    
    
}